/*
 * @Author: your name
 * @Date: 2019-10-21 09:08:46
 * @LastEditTime: 2019-10-29 15:40:13
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /c:\Users\DELL\Desktop\zhhu\app\Router\Book.js
 */
const jwt= require('koa-jwt')
const Router = require('koa-router');
const router=new Router({prefix:'/user'});
const {secret} =require('../config');
const {find, findById, create,update,delete:del
    ,login,checkOwner,listFollowing,following,listQuestions
    ,unfollowing,listFollowers,chexkUserExist,unfollowingTopic,followingTopic,listFollowingTopic
} =require('../controllers/User')
const { checkTopicExist }=require("../controllers/topics")
// const auth =async(ctx,next)=>{
    
//     const { authorization ="" } = ctx.request.header;
//     const token = authorization.replace('Bearer ','');
//     try{
//         const user = jsonwebtoken.verify(token,secret);
//         ctx.state.user=user
//     }catch(err){
//         ctx.throw(401,err.message)
//     }
//     await next()
// }
const auth =jwt({secret});

router.get('/',find)//用户列表
router.post('/',create)//创建用户
router.get('/:id',findById)//查询用户
router.patch('/:id',auth,checkOwner,update)//更新用户
router.delete('/:id',auth,checkOwner,del)//删除
router.post('/login',login)//登录
router.get('/:id/following',listFollowing)//获取关注
router.put('/following/:id',auth,chexkUserExist,following)//关注
router.delete('/following/:id',auth,chexkUserExist,unfollowing)//取消关注
router.get('/:id/followers',listFollowers)//获取粉丝列表
router.put('/followingTopic/:id',auth,checkTopicExist,followingTopic)//关注话题
router.delete('/unfollowingTopic/:id',auth,checkTopicExist,unfollowingTopic)//取消关注话题
router.get('/:id/listFollowingTopic',listFollowingTopic)//获取某人关注的话题
router.get('/:id/questions',listQuestions)
module.exports=router;